/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.gui;

import java.io.File;
import java.text.MessageFormat;

/**
 * This class contains methods to retrieve application folders for various platforms.
 * This file is platform aware. 
 *
 * @author Basil Shikin
 *
 */
public class ApplicationDataLocator
{
    private static final String COMPANY_NAME = "bugstat";
    private static final String APP_NAME     = "bugstat";
    private static final String APP_VERSION     = "1.0";
    private static final String APP_PATH     = "org.bugstat";
    
    public static File getApplicationDataFolder()
    {
        final File result;
        if ( isWindows() )
        {
            result = new File( MessageFormat.format("{0}\\{1}\\{2}", System.getenv("APPDATA"), COMPANY_NAME, APP_NAME ) );
        }
        else if ( isOSX() )
        {
            result = new File(MessageFormat.format("{0}/Library/Application Support/{1}", System.getenv("HOME"), APP_NAME ) );
        }
        else if ( isUnix() ) 
        {
            result = new File(MessageFormat.format("{0}/.{1}/{2}", System.getenv("HOME"), COMPANY_NAME, APP_NAME ));
        }
        else
        {
            throw new RuntimeException("Unsupported operating system: " + System.getProperty("os.name") );
        }
        
        if ( !result.exists() ) result.mkdirs();
        
        return result;
    }
    
    public static File getCacheFolder()
    {
        final File result;
        if ( isWindows() )
        {
            result = new File( MessageFormat.format("{0}\\{1}\\{2}\\{3}.default\\Cache", System.getenv("APPDATA"), COMPANY_NAME, APP_NAME, APP_VERSION ) );
        }
        else if ( isOSX() )
        {
            result = new File(MessageFormat.format("{0}/Library/Caches/{1}/{2}.default", System.getenv("HOME"), APP_PATH, APP_VERSION ) );
        }
        else if ( isUnix() )
        {
            result = new File(MessageFormat.format("{0}/.{1}/{2}/{3}.default/Cache", System.getenv("HOME"), COMPANY_NAME, APP_NAME, APP_VERSION ) );
        }
        else
        {
            throw new RuntimeException("Unsupported operating system: " + System.getProperty("os.name") );
        }
        
        if ( !result.exists() ) result.mkdirs();
        
        return result;
    }
 
    private static boolean isWindows()
    {
        final String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf( "win" ) >= 0); 
    }
 
    private static boolean isOSX()
    {
 
        final String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf( "mac" ) >= 0); 
    }
 
    private static boolean isUnix()
    {
        final String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0);
    }
}
